<?php

namespace App\Http\Controller\AdminPort;

use App\Model\AuthAccountModel;
use App\Model\CompanyModel;
use App\Model\EmployeeModel;
use App\Service\CompanyService;
use Hyperf\Di\Annotation\Inject;


/**
 *  企业管理
 * @package App\Http\Controller\AdminPort
 */
class Company
{
    #[Inject]
    protected CompanyService $companyService;

    /**
     * 获取详细信息
     * @return array
     * @throws \Exception
     */
    public function get(): array
    {
        CompanyModel::validateColumn(
            [],
            [
                CompanyModel::C_ID,
            ]
        );

        $company = $this->companyService->getInfo(I(CompanyModel::C_ID));
        return getResponseData('', $company);
    }

    /**
     * 获取分页列表
     * @return string[]
     */
    public function getListPage()
    {
        CompanyModel::validateColumn(
            [
                CompanyModel::C_NAME,
                CompanyModel::C_MOBILE,
                CompanyModel::C_PERSON_IN_CHARGE,
                CompanyModel::C_DOMAIN,
                CompanyModel::C_TEL,
                CompanyModel::C_STATUS
            ]
        );

        $params = [
            CompanyModel::C_NAME => I(CompanyModel::C_NAME),
            CompanyModel::C_MOBILE => I(CompanyModel::C_MOBILE),
            CompanyModel::C_PERSON_IN_CHARGE => I(CompanyModel::C_PERSON_IN_CHARGE),
            CompanyModel::C_DOMAIN => I(CompanyModel::C_DOMAIN),
            CompanyModel::C_TEL => I(CompanyModel::C_TEL),
            CompanyModel::C_STATUS => I(CompanyModel::C_STATUS),
            CompanyModel::C_TYPE => 2
        ];

        $data = $this->companyService->getListByPage($params, 15);
        return getResponseData('', $data);
    }

    /**
     * 创建企业
     */
    public function create()
    {
        return $this->saveCompany(true);
    }

    /**
     * 修改企业信息
     * @return array
     */
    public function update()
    {
        return $this->saveCompany(false);
    }

    /**
     * 创建和编辑企业信息
     * @param bool $isCreate
     * @return array
     */
    protected function saveCompany(bool $isCreate = false)
    {
        $required = [
            CompanyModel::C_NAME,
            CompanyModel::C_MOBILE,
            CompanyModel::C_EXPIRE_DATE,
            CompanyModel::C_STATUS
        ];

        if ( ! $isCreate) {
            $required[] = CompanyModel::C_ID;
        }

        $comany = CompanyModel::validateColumn([
                    CompanyModel::C_DOMAIN,
                    CompanyModel::C_BUSINESS_LICENSE,
                    CompanyModel::C_PERSON_IN_CHARGE,
                    CompanyModel::C_TEL,
                    CompanyModel::C_PROVINCE_ID,
                    CompanyModel::C_CITY_ID,
                    CompanyModel::C_COUNTY_ID
                ],
                $required,
                true
        );

        AuthAccountModel::validateColumn([
            AuthAccountModel::C_PASSWORD
        ]);

        $this->companyService->save($comany, I(AuthAccountModel::C_PASSWORD));
        return getResponseData('保存成功');
    }

    /**
     * 删除企业
     * @return string[]
     */
    public function delete()
    {
        CompanyModel::validateColumn([], [CompanyModel::C_ID]);
        $this->companyService->delete(I(CompanyModel::C_ID));
        return getResponseData('删除成功');
    }
}
